<?php

class Model_Orm_Ticket_Tests extends \PHPUnit_Framework_TestCase
{
    public function test_tck_1() {
        $data = array(
            'First_name' => 'John',
            'Last_name' => 'Doe',
            'Email' => 'john@gmail.com',
            'Phone' => '26934563',
            'Show_ID' => 1,
        );
        
        $seat_info = array(
            1 => array('row' => 1, 'seat' => 1),
            2 => array('row' => 1, 'seat' => 2),
            3 => array('row' => 1, 'seat' => 3),
        );
        
        $validation = Model_Orm_Order::validate('add_tck_1');
        if($validation->run($data)) {
            $new_order = new Model_Orm_Order();
            $new_order->first_name = $validation->validated("First_name");
            $new_order->last_name = $validation->validated("Last_name");
            $new_order->email = $validation->validated("Email");
            $new_order->phone_nr = $validation->validated("Phone");
            $new_order->user_id = 1;
            $new_order->show_id = $validation->validated("Show_ID");
            $new_order->purchase_time = time();
            $new_order->is_paid = 0;
            $new_order->save();

            $newest_order_id = Model_Orm_Order::query()->max('id');

            foreach($seat_info as $seat) {
                $new_reservation = new Model_Orm_Reservation();
                $new_reservation->order_id = $newest_order_id;
                $new_reservation->row = $seat['row'];
                $new_reservation->seat = $seat['seat'];

                $new_reservation->save();
            }
        } else {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        }
    }
    
    public function test_tck_2() {
        $all_shows = array(
            array('movie_id' => 1, 'auditorium_id' => 1, 'start_time' => 1402056000, 'end_time' => 1402063200, 'price' => 4.99)
        );

        foreach($all_shows as $data) {
            $show = \Model_Orm_Show::forge();
            $show->movie_id = $data['movie_id'];
            $show->auditorium_id = $data['auditorium_id'];
            $show->start_time = $data['start_time'];
            $show->end_time = $data['end_time'];
            $show->price = $data['price'];
            $show->save();
        }
        
        $all_orders = array(
            array('show_id' => 1, 'user_id' => 2, 'first_name' => 'John', 'last_name' => 'Doe', 'email' => 'john.doe@mail.com', 'phone_nr' => '12345678', 'is_paid' => 1, 'purchase_time' => 1401708600)
        );

        foreach($all_orders as $data) {
            $order = \Model_Orm_Order::forge();
            $order->show_id = $data['show_id'];
            $order->user_id = $data['user_id'];
            $order->first_name = $data['first_name'];
            $order->last_name = $data['last_name'];
            $order->email = $data['email'];
            $order->phone_nr = $data['phone_nr'];
            $order->is_paid = $data['is_paid'];
            $order->purchase_time = $data['purchase_time'];
            $order->save();
        }
        
        $all_reservations = array(
            array(1,1,1), array(1,1,2)
        );

        foreach($all_reservations as $data) {
            $reservation = \Model_Orm_Reservation::forge();
            $reservation->order_id = $data[0];
            $reservation->row = $data[1];
            $reservation->seat = $data[2];
            $reservation->save();
        }

        $data = array(
            'First_name' => 'John',
            'Last_name' => 'Doe',
            'Email' => 'john@gmail.com',
            'Phone' => '26934563',
            'Show_ID' => 1,
        );
        
        $seat_info = array(
            1 => array('row' => 1, 'seat' => 1),
            2 => array('row' => 1, 'seat' => 2),
            3 => array('row' => 1, 'seat' => 3),
        );
        
        $show_obj = Model_Orm_Show::find('last');
        $show = $show_obj->id;
        
        if(is_null($show)) {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        }
        
        $show_info = Model_Orm_Show::find($show);
        
        if(is_null($show_info)) {
            throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
        }
        
        $orders_info = $show_info->get('orders');
        
        $reserved_seats = array();
        
        foreach($orders_info as $order) {
            $reservation_info = $order->get('reservations');
            
            foreach($reservation_info as $reservation) {
                array_push($reserved_seats, array('row' => $reservation['row'], 'seat' => $reservation['seat']));
            }
        }

        $unavailable_seats = array();

        foreach($seat_info as $chosen_seat) {
            foreach($reserved_seats as $reserved_seat) {
                if($reserved_seat['row'] == $chosen_seat['row'] && $reserved_seat['seat'] == $chosen_seat['seat']) {
                    array_push($unavailable_seats, array('row' => $chosen_seat['row'], 'seat' => $chosen_seat['seat']));
                }
            }
        }

        if(count($unavailable_seats) === 0) {
            $validation = Model_Orm_Order::validate('add_tck_2');
            if($validation->run($data)) {
                $new_order = new Model_Orm_Order();
                $new_order->first_name = $validation->validated("First_name");
                $new_order->last_name = $validation->validated("Last_name");
                $new_order->email = $validation->validated("Email");
                $new_order->phone_nr = $validation->validated("Phone");
                $new_order->user_id = 1;
                $new_order->show_id = $validation->validated("Show_ID");
                $new_order->purchase_time = time();
                $new_order->is_paid = 0;
                $new_order->save();

                $newest_order_id = Model_Orm_Order::query()->max('id');

                foreach($seat_info as $seat) {
                    $new_reservation = new Model_Orm_Reservation();
                    $new_reservation->order_id = $newest_order_id;
                    $new_reservation->row = $seat['row'];
                    $new_reservation->seat = $seat['seat'];

                    $new_reservation->save();
                }
                echo "* Test TCK 2 - OK!";
                
            } else {
                throw new Orm\ValidationFailed(implode(' * ', e((array) $validation->error())));
            }
        } else {
            echo "* Test TCK 2 - OK!";
        }
    }
}

